import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import type { XYCoord } from '../../classes/interface';
//存放柜当前正在拖放的独一份数据

export interface OperationState {
  //drag
  itemType?: string;
  item?: any;
  sourceId?: string;
  dragStartNodeClientOffset?: XYCoord | null;
  dragStartEventClientOffset?: XYCoord | null;
  //drop
  targetId?: string;
  dropEventClientOffset?: XYCoord | null;
  dropNodeClientOffset?: XYCoord | null;
  dropResult?: any;
  isSourcePublic?: boolean;
}

const dragOperationSlice = createSlice({
  name: 'dragOperationStore',
  initialState: () => ({}),
  reducers: {
    beginDrag: (state, action: PayloadAction<OperationState>) => {
      return action.payload;
    },
    publishDragSource: (state, action: PayloadAction<OperationState>) => {
      return {
        ...state,
        isSourcePublic: true,
      };
    },
    over: (state, action: PayloadAction<OperationState>) => {
      return {
        ...state,
        ...action.payload,
      };
    },
    updateItem: (state, action: PayloadAction<OperationState>) => {
      const newState = {
        ...state,
        item: action.payload,
      };
      return newState;
    },
    drop: (state, action: PayloadAction<OperationState>) => {
      return {
        ...state,
        ...action.payload,
      };
    },
    endDrag: (state, action: PayloadAction<OperationState>) => {
      return action.payload;
    },
  },
});

export default dragOperationSlice;
